DeltaPreserve .equ appbackupscreen + 765
;For debugging
StackPointer .equ appbackupscreen + 700
NumberPreserve .equ appbackupscreen + 759
ASCIINumber .equ appbackupscreen + 753

bcd16 = NumberPreserve
bcd16string = ASCIINumber

Rotate_Select_Mode:

 SET Wait_Between_Presses, (IY + asm_flag1)

 BIT Select_Mode, (IY + asm_flag1)
 JR z, _
 RES Select_Mode, (IY + asm_flag1)
 ld de, (DeltaPreserve)
 ld (delta), de

 RET

_
 
 SET Select_Mode, (IY + asm_flag1)
 ld a, 48
 ld (Select_Cursor_X),a
 ld a, 16
 ld (Select_Cursor_Y),a
 ld de, (delta)
 ld (DeltaPreserve), de

 ld de, 16
 ld (delta), de

Adjust_Map_X_Y_For_Cursor:

 ld hl, (MapX)

 BIT 3,l
 jr z, _
 ld a, l
 and %11110000
 
 add a, 16
 ld l, a

 ld (MapX), hl 

 jr ++_

_

 ld a,l
 and %11110000
 ld l, a
 ld (MapX), hl 

_


 ld hl, (MapY)
 BIT 3,l
 jr z, _
 ld a, l
 and %11110000

 add a, 16
 ld l, a

 ld (MapY), hl 

 RET
_
 ld a, l
 and %11110000
 ld l, a
 ld (MapY), hl 


 RET

Move_Select_Cursor_Down:
 SET Wait_Between_Presses, (IY + asm_flag1)
 ld a, (Select_Cursor_Y)

 cp 48
 jr z, _
 add a, 16
 ld (Select_Cursor_Y), a

 ret

_
 
 CALL movemapdown
 ret

Move_Select_Cursor_Up:
 SET Wait_Between_Presses, (IY + asm_flag1)
 ld a, (Select_Cursor_Y)

 or a
 jr z, _
 sub 16
 ld (Select_Cursor_Y),a

 ret

_

 CALL movemapup
 ret

Move_Select_Cursor_Left:
 SET Wait_Between_Presses, (IY + asm_flag1)
 ld a, (Select_Cursor_X)

 or a
 jr z, _
 sub 16
 ld (Select_Cursor_X),a

 ret

_

 CALL movemapleft
 ret



Move_Select_Cursor_Right:
 SET Wait_Between_Presses, (IY + asm_flag1)
 ld a, (Select_Cursor_X)

 cp 80
 jr z, _
 add a, 16
 ld (Select_Cursor_X),a

 ret

_


 CALL movemapright
 ret


Draw_Select_Cursor:

 BIT Object_Ready_To_Add, (IY + asm_flag1)
 jp nz, ++_

 di


 ld b, 16
 ld c, 2
 ld a, (Select_Cursor_X)
 ld d, a
 ld a, (Select_Cursor_Y)
 ld e, a
 ld ix, Select_Cursor
 ld iy, Select_Cursor_Mask
 call LargeClippedSprite
 ld iy, flags
 ei



 call Find_If_Tile_Occupied_By_Building

 or a
 jr z, _
 
 call Draw_Building_Info


 ret

_

 ld hl, plotsscreen
 call safeCopy

 ret 

_


 ld hl, Structure_Graphics_Addresses
 ld d,0
 ld a, (BuildingSelected)
 ld e,a
  
 add hl, de
 add hl, de
 ld e, (hl)
 inc hl
 ld d, (hl)
 ex de, hl


 ld c, (hl)

 inc hl
 ld b, (hl)

 inc hl
 ld a, (Select_Cursor_X)
 ld d, a
 ld a, (Select_Cursor_Y)
 ld e, a
 ld a, (hl)
 inc hl
 push hl
 push de
 ld d,0
 ld e,a
 add hl, de
 push hl
 di
 pop iy
 pop de
 pop ix
 call LargeClippedSprite
 ld iy, flags
 ei
 ld hl, plotsscreen
 call safeCopy
 ret 

Select_Cursor:

	.db $00,$00
	.db $01,$00
	.db $02,$80
	.db $05,$40
	.db $05,$40
	.db $1D,$70
	.db $21,$08
	.db $5E,$F4
	.db $21,$08
	.db $1D,$70
	.db $05,$40
	.db $05,$40
	.db $02,$80
	.db $01,$00
	.db $00,$00
	.db $00,$00

Select_Cursor_Mask:

	.db %11111111,%11111111
	.db %11111110,%11111111
	.db %11111100,%01111111
	.db %11111000,%00111111
	.db %11111000,%00111111
	.db %11100000,%00001111
	.db %11000000,%00000111
	.db %10000000,%00000011
	.db %11000000,%00000111
	.db %11100000,%00001111
	.db %11111000,%00111111
	.db %11111000,%00111111
	.db %11111100,%01111111
	.db %11111110,%11111111
	.db %11111111,%11111111
	.db %11111111,%11111111


Draw_Building_Info:

 push ix
 ld ix, InfoBox
 ld A, 12
 ld (Sprite_Width), A
 ld A, 0
 ld l, 48
 ld b, 15
 call Put_Sprite_Overwrite
  ld hl, plotsscreen
 call safeCopy

 pop ix
 ld a, (IX + 4)
 ld hl, Operation_Center
 or a
 ld de, 23
 sbc hl, de
 ld b,a
 inc b
_

 add hl, de
 djnz -_
 ld de, 4
 add hl, de
 push hl

 LD hl,49*256+2
 LD (penCol),hl
 pop hl
 LD DE, OP1
 LD BC, 19
 LDIR
 LD HL, OP1
 B_CALL _VPutS

 DrawSmallTextVariableLength(2,55,Friendly_Unit,9)
 DrawSmallTextVariableLength(62,55,HP_Text,4)


 
 ld e, (IX+5)
 ld d, (IX + 6)
 call bintobcd16

 DrawSmallTextVariableLength(78,55,ASCIINumber,6)
 
 

 ret

InfoBox:

.db %11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111
.db %10000000,%00000000,%00000000,%00000000,%00000000,%00000000,%00000000,%00000000,%00000000,%00000000,%00000000,%00000001
.db %10000000,%00000000,%00000000,%00000000,%00000000,%00000000,%00000000,%00000000,%00000000,%00000000,%00000000,%00000001
.db %10000000,%00000000,%00000000,%00000000,%00000000,%00000000,%00000000,%00000000,%00000000,%00000000,%00000000,%00000001
.db %10000000,%00000000,%00000000,%00000000,%00000000,%00000000,%00000000,%00000000,%00000000,%00000000,%00000000,%00000001
.db %10000000,%00000000,%00000000,%00000000,%00000000,%00000000,%00000000,%00000000,%00000000,%00000000,%00000000,%00000001
.db %10000000,%00000000,%00000000,%00000000,%00000000,%00000000,%00000000,%00000000,%00000000,%00000000,%00000000,%00000001
.db %10000000,%00000000,%00000000,%00000000,%00000000,%00000000,%00000000,%00000000,%00000000,%00000000,%00000000,%00000001
.db %10000000,%00000000,%00000000,%00000000,%00000000,%00000000,%00000000,%00000000,%00000000,%00000000,%00000000,%00000001
.db %10000000,%00000000,%00000000,%00000000,%00000000,%00000000,%00000000,%00000000,%00000000,%00000000,%00000000,%00000001
.db %10000000,%00000000,%00000000,%00000000,%00000000,%00000000,%00000000,%00000000,%00000000,%00000000,%00000000,%00000001
.db %10000000,%00000000,%00000000,%00000000,%00000000,%00000000,%00000000,%00000000,%00000000,%00000000,%00000000,%00000001
.db %10000000,%00000000,%00000000,%00000000,%00000000,%00000000,%00000000,%00000000,%00000000,%00000000,%00000000,%00000001
.db %10000000,%00000000,%00000000,%00000000,%00000000,%00000000,%00000000,%00000000,%00000000,%00000000,%00000000,%00000001
.db %11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111

Friendly_Unit:

 .db "Friendly",0

HP_Text:
 
 .db "HP:",0